//   2025.07.13 13:55:55 星期天 晴 无事

import { ObjectOnlyPushSet } from '../../../../ObjectOnlyPushSet_operate';
import {
  diff_prop,
  keyWorld_compared_type,
} from '../../../Classes/cla_diff_prop';
import { prop_static_name_prefix } from '../../../DataDefine/sourceData';
import { cur_compare_type } from '../Types';

// 这个是 对 数组 比对 结果 相似度 进行评分 罗列

const deepth_diff_typeTag = 'deepth_diff_type';

const prop_static_cur_compare_goals = `${prop_static_name_prefix}_compare_goals`;

const prop_static_cur_compare_describe = `${prop_static_name_prefix}_compare_describe`;

/**
 * 比对结果 分级 分数
 */
const goals_compare_type = {
  increase: -10,
  delete: -10,
  static: 100,
  all_same: 100,
  val_no_same: -1,
  no_same: -10,
  update: -10,
  more_same: -1,
};

export type typ_key_goals_compare_type = keyof typeof goals_compare_type;

type typ_same_noSame = [0 | 1, 0 | 1];

export const same_describe = {
  allSame: 'all_same',
  noSame: 'no_same',
  moreSame: 'more_same',
  valNoSame: 'val_no_same',
} as const;

// export type same_describe = 'allSame' | 'NoSame' | 'moreSame' | 'valueNoSame';

export type typ_val_same_describe =
  (typeof same_describe)[keyof typeof same_describe];

class deepth_diff_type {
  get [Symbol.toStringTag]() {
    return deepth_diff_typeTag;
  }

  [_: string]:
    | string[]
    | number
    | typ_key_goals_compare_type
    | deepth_diff_type;

  constructor(cur_compare: cur_compare_type) {
    /**
     * 当前 比对结果
     */
    let cur_describe: typ_val_same_describe;

    /**
     * 当前 分数 总计
     */
    let total_goals: number = 0;

    /**
     * 当前 相同项 和 不同项 评判个数
     */
    let cur_total_describe_arr: typ_same_noSame = [0, 0];

    // 基础 比对类型
    if (cur_compare instanceof diff_prop) {
      this[prop_static_cur_compare_goals] =
        goals_compare_type[cur_compare._$223_compare_type];

      this[prop_static_cur_compare_describe] = cur_compare._$223_compare_type;
      return;
    }

    for (const key_ind of cur_compare.data) {
      const child_cur_compare = <cur_compare_type>cur_compare[key_ind];

      let child_deepth_diff_type = new deepth_diff_type(child_cur_compare);

      ObjectOnlyPushSet(this, key_ind, child_deepth_diff_type);

      let child_describe =
        child_deepth_diff_type[prop_static_cur_compare_describe];

      total_goals += getcurDescribe_goals(child_describe);

      set_cur_describe(cur_total_describe_arr, child_describe);
    }

    // 当前 遍历 子元素 相似度 最终描述
    cur_describe = async_cur_describe(cur_total_describe_arr);

    this[prop_static_cur_compare_goals] = total_goals;

    this[prop_static_cur_compare_describe] = cur_describe;
  }

  data: string[] = [];

  /**
   * 当前 比对分数
   */
  [prop_static_cur_compare_goals]: number;

  /**
   * 当前 相似度 描述
   */
  [prop_static_cur_compare_describe]: typ_key_goals_compare_type;
}

/**
 * 根据 比对描述 进行汇总 描述
 * @param {[0,0]} cur_total_describe_arr
 * @param {typ_key_goals_compare_type} describe 描述 汇总
 */
function set_cur_describe(
  cur_total_describe_arr: [number, number],
  describe: typ_key_goals_compare_type
) {
  let init = (NoSame: number, Same: number) => {
    if (cur_total_describe_arr[0] == 0 && NoSame) {
      cur_total_describe_arr[0] = 1;
    }
    if (cur_total_describe_arr[1] == 0 && Same) {
      cur_total_describe_arr[1] = 1;
    }
  };

  let NoSame = 1;
  let Same = 1;

  switch (describe) {
    case keyWorld_compared_type.static:
      init(0, Same);
      break;
    case keyWorld_compared_type.increase:
      init(NoSame, 0);
      break;
    case keyWorld_compared_type.delete:
      init(NoSame, 0);
      break;
    case keyWorld_compared_type.valueNoSame:
      init(NoSame, Same);
      break;
    case same_describe.allSame:
      init(0, Same);
      break;
    case same_describe.moreSame:
      init(NoSame, Same);
      break;
    case same_describe.noSame:
      init(NoSame, 0);
      break;
    case same_describe.valNoSame:
      init(NoSame, Same);
      break;

    default:
      break;
  }
}

function async_cur_describe(cur_describe: typ_same_noSame) {
  let goals = <0 | 1 | 2 | 3>(cur_describe[0] * 2 + cur_describe[1]);
  switch (goals) {
    case 0:
      return same_describe.valNoSame;
    case 1:
      return same_describe.allSame;
    case 2:
      return same_describe.noSame;
    case 3:
      return same_describe.moreSame;
  }
}

/**
 * 根据 描述 返回 分数
 * @param {typ_key_goals_compare_type} cur_describe
 * @returns {number}
 */
function getcurDescribe_goals(cur_describe: typ_key_goals_compare_type) {
  return goals_compare_type[cur_describe];
}

export { deepth_diff_type };
